package edu.neu.chenhan.fragment.cooperation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import edu.neu.chenhan.R;
import edu.neu.chenhan.adapter.CooperationCategoryAdapter;
import edu.neu.chenhan.listener.OnItemClickListener;
import edu.neu.chenhan.model.CooperationCategory;
import edu.neu.chenhan.util.MockDataUtil;

import java.util.List;

public class CooperationHomeFragment extends Fragment implements OnItemClickListener {

    private RecyclerView rvCooperationCategories;
    private CooperationCategoryAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cooperation_home, container, false);

        // 初始化RecyclerView
        rvCooperationCategories = view.findViewById(R.id.rv_cooperation_categories);
        rvCooperationCategories.setLayoutManager(new LinearLayoutManager(getContext()));

        // 获取模拟数据
        List<CooperationCategory> categories = MockDataUtil.getCooperationCategories();
        adapter = new CooperationCategoryAdapter(getContext(), categories, this);
        rvCooperationCategories.setAdapter(adapter);

        return view;
    }

    @Override
    public void onItemClick(int position) {
        // 根据type导航到对应的片段
        Fragment targetFragment = null;
        CooperationCategory category = adapter.getItem(position);
        switch (category.getType()) {
            case 0: // 会议研讨
                targetFragment = new MeetingListFragment();
                break;
            case 1: // 标准定制
                targetFragment = new StandardDetailFragment();
                break;
            case 2: // 技术培训
                targetFragment = new TrainingDetailFragment();
                break;
            case 3: // 工具研发
                targetFragment = new ToolDetailFragment();
                break;
            case 4: // 公益行动
                targetFragment = new WelfareFragment();
                break;
        }

        if (targetFragment != null) {
            requireActivity().getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, targetFragment)
                    .addToBackStack(null)
                    .commit();
        }
    }
}